﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Business;
using Entities;
using DevComponents.DotNetBar;
namespace Presentation
{
    public partial class frmThayDoiQuyDinh : DevComponents.DotNetBar.Office2007Form
    {
        DataView dv = new DataView();
        public frmThayDoiQuyDinh()
        {
            InitializeComponent();
            DataGridViewCheckBoxColumn suDung = new DataGridViewCheckBoxColumn(false);
            suDung.Name = "SuDung";
            suDung.DataPropertyName = "DuocSuDung";
            dgvThamSo.Columns.Add(suDung);
        }

        private void frmThayDoiQuyDinh_Load(object sender, EventArgs e)
        {
            
            dgvThamSo.AutoGenerateColumns = false;
            dgvThamSo.Columns.Add("MaThamSo", "Mã tham số");
            dgvThamSo.Columns["MaThamSo"].DataPropertyName = "MaThamSo";
            dgvThamSo.Columns["MaThamSo"].ReadOnly = true;

            dgvThamSo.Columns.Add("TenThamSo", "Tên tham số");
            dgvThamSo.Columns["TenThamSo"].DataPropertyName = "TenThamSo";
            dgvThamSo.Columns["TenThamSo"].ReadOnly = true;
            dgvThamSo.Columns.Add("GiaTri", "Giá trị");
            dgvThamSo.Columns["GiaTri"].DataPropertyName = "GiaTri";

            

            dgvThamSo.Columns["SuDung"].DisplayIndex = dgvThamSo.Columns.Count - 1;
            dgvThamSo.Columns["SuDung"].HeaderText = "Sử dụng";


            LoadGridView();
            
        }

        private void btnThayDoi_Click(object sender, EventArgs e)
        {
            ThayDoi();
        }

       
        private void dgvThamSo_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            MessageBoxEx.Show("Nhập liệu sai định dạng", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            LoadGridView();
            btnThayDoi.Enabled = true;
        }
        private void LoadGridView()
        {
            DataTable dt = ThamSoService.GetThamSoTable();
            dt.Columns.Add("DuocSuDung", typeof(bool));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["SuDung"].ToString() == "1")
                    dt.Rows[i]["DuocSuDung"] = true;
                else
                    dt.Rows[i]["DuocSuDung"] = false;
            }
            dt.TableName = "ThamSo";
            dt.AcceptChanges();
            dv.Table = dt;
            dgvThamSo.DataSource = dt;
        }
        private bool ThayDoi()
        {
            dv.RowStateFilter = DataViewRowState.ModifiedCurrent;
            DataTable dtCapNhat = dv.ToTable();


            for (int i = 0; i < dtCapNhat.Rows.Count; i++)
            {
                ThamSoDTO ts = new ThamSoDTO();
                ts.MaThamSo = int.Parse(dtCapNhat.Rows[i]["MaThamSo"].ToString());
                ts.GiaTri = double.Parse(dtCapNhat.Rows[i]["GiaTri"].ToString());
                ts.SuDung = bool.Parse(dtCapNhat.Rows[i]["DuocSuDung"].ToString()) == true ? 1 : 0;
                ThamSoService.Update(ts);
            }
            MessageBoxEx.Show("Đã thay đổi quy định", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            btnThayDoi.Enabled = false;
            return true;
        }
        private void frmThayDoiQuyDinh_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (btnThayDoi.Enabled == true)
            {
                DialogResult r = MessageBoxEx.Show("Bạn có muốn thay đổi quy định?", "Thông báo!", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (r == DialogResult.Cancel)
                {
                    e.Cancel = true;

                }
                else if (r == DialogResult.Yes)
                {
                    ThayDoi();                        
                }
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
